<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class SelectedGoodsLog extends Model
{
    protected $table ="c_selected_goods_logs";


    public function goods()
    {
        return $this->hasOne('App\Models\ProductGoods', 'id', 'goods_id');
    }

    public function course()
    {
        return $this->hasOne('App\Models\Course', 'id', 'goods_id');
    }

    public function article()
    {
        return $this->hasOne('App\Models\Article', 'id', 'goods_id');
    }

    //1商品2课程3公益4咨询
    public function getWorkRightsAndInterests($data)
    {
        if(isset($data['g_ids']) && count($data['g_ids']) >0){
            $select_num = count($data['g_ids']);
        }else{
            $select_num = self::where(['type'=>$data['type'],'work_id'=>$data['work_id']])->count("id");
        }
        $work = DoctorWorks::where(['id'=>$data['work_id']])->first();
        if($data['type'] == 1){ //商品
            $num = WorkRelateGoods::
            from("c_work_relate_goods as relate_goods")
                ->join("c_product_goods as goods", 'goods.id', '=', 'relate_goods.goods_id')
                ->where([['goods.supplier_work_id', '!=', $data['work_id']], ['relate_goods.work_id', '=',  $data['work_id']]])
                ->count("goods.id");
            $all = $num + $select_num;
            if($work->medal_type ==0){
                if($all >10){
                    return false;
                }
            }elseif ($work->medal_type ==1){
                if($all >20){
                    return false;
                }
            }elseif ($work->medal_type ==2){
                return true;
            }
        }elseif ($data['type'] == 2){ //课程
            $num = WorkRelateCourse::
            from("c_work_relate_courses as relate_course")
                ->join("c_courses as course", 'course.id', '=', 'relate_course.course_id')
                ->where([['course.work_id', '!=', $data['work_id']], ['relate_course.work_id', '=',  $data['work_id']]])
                ->count("course.id");
            $all = $num + $select_num;
            if($work->medal_type ==0){
                if($all >3){
                    return false;
                }
            }elseif ($work->medal_type ==1){
                if($all >6){
                    return false;
                }
            }elseif ($work->medal_type ==2){
                return true;
            }
        }else{
            return true;
        }
        return true;
    }



    /***
     * 添加选品记录
     *
     */
    public function addSelectedGoodsLog($param)
    {
        $res = self::getInfos($param);
        if ($res) {
            return true;
        }
        //判断该工作室权益
        $dec = $this->getWorkRightsAndInterests($param);
        if ($dec) {
            $this->goods_id = $param['goods_id'];
            $this->work_id = $param['work_id'];
            $this->type = $param['type'];
            return $this->save();
        } else {
            return "您的工作室等级还不够,不能选中更多内容!";
        }
    }
   /***
     * 删除选品记录
     *
     */
    public function delSelectedGoodsLog($param)
    {
        $res = $this->where([["goods_id", '=', $param['goods_id']], ["work_id", '=', $param['work_id']], ["type", '=', $param['type']]])->delete();
        return $res;
    }

    /**
     * @param $param
     * 1商品2课程3工艺4咨询
     * 获取选品log
     */
    public function getSelectedGoodsLog($param)
    {
        if($param['type'] ==1){
            $with = ['goods'];
        }elseif ($param['type'] ==2){
            $with = ['course'];
        }elseif ($param['type'] ==3){
            $with = ['article'];
        }
        $res = self::where([['work_id','=',$param['work_id']],['type','=',$param['type']]])->with($with)->get();
        if (!empty($res)) {
            if ($param['type'] == 1) {
                foreach ($res as $k => &$v) {
                    $v->goods->product_image = Url::getHttpHost() . 'upload/' . $v->goods->product_image;
                }
            } elseif ($param['type'] == 2) {
                foreach ($res as $k => &$v) {
                    $v->course->course_img = Url::getHttpHost() . 'upload/' . $v->course->course_img;
                }
            } elseif ($param['type'] == 3) {
                foreach ($res as $k => &$v) {
                    $v->article->art_img = Url::getHttpHost() . 'upload/' . $v->article->art_img;
                }
            }
           return $res->toArray();
        }
      return [];
    }
    
    /**
     * @param $filter
     * @return mixed
     * 获取记录信息
     */
    public static function getInfos($filter)
    {
        return self::where([['goods_id', '=', $filter['goods_id']], ['work_id', '=', $filter['work_id']], ['type', '=', $filter['type']]])->first();
    }

    /***
     * @param $filter
     * 获取已选择的公益文章/已选课程/已选商品
     */
    public static function getSelectedArticleIds($filter,$type)
    {
        return self::where([['work_id', '=', $filter['work_id']],['type', '=', $type]])->pluck("goods_id");
    }
    
}
